﻿using Microsoft.Xna.Framework;
using System.Collections.Generic;
using XNAFight.Engine.Inputs;
using System;

namespace XNAFight.Scene.Core
{
    public abstract class MenuScreen : Screen
    {
        #region CHAMPS

        private string title;
        protected Vector2 title_pos;
        protected MenuItem[,] items;
        protected int selection_x, selection_y;
        protected int default_selection_x, default_selection_y;
        
        #endregion
        
        #region PROPRIETES
        
        #endregion
        
        #region INITIALISATION

        public MenuScreen(string _title)
            : base()
        {
            items = null;
            selection_x = 0;
            selection_y = 0;
            default_selection_x = 0;
            default_selection_y = 0;
            title = _title;
            title_pos = new Vector2(20, 20);
        }
        #endregion

        #region UPDATE / DRAW
        
        public override void Update(GameTime _game_time)
        {
            base.Update(_game_time);

            if (items != null)
            {
                // MaJ des éléments de menu
                for (int x = 0; x < items.GetLength(0); x++)
                {
                    for (int y = 0; y < items.GetLength(1); y++)
                    {
                        if (items[x, y] != null)
                            items[x, y].Update(_game_time, x == selection_x && y == selection_y);
                    }
                }
            }
        }

        public override void Draw(GameTime _game_time)
        {
            CommonContent.SpriteBatch.Begin();

            // Affichage de l'intitulé
            CommonContent.SpriteBatch.DrawString(CommonContent.DefaultFont, title, title_pos, Color.Black);
            if (items != null)
            {
                // Affichage des éléments de menu
                for (int x = 0; x < items.GetLength(0); x++)
                {
                    for (int y = 0; y < items.GetLength(1); y++)
                    {
                        if (items[x, y] != null)
                            items[x, y].Draw(_game_time, x == selection_x && y == selection_y);
                    }
                }
            }

            CommonContent.SpriteBatch.End();
        }
        
        #endregion

        #region AUTRES METHODES
        
        public override void Reset()
        {
            selection_x = default_selection_x;
            selection_y = default_selection_y;
        }

        public override void HandleInputs()
        {
            if (items != null)
            {
                // Selection de l'element suivant dans le menu en allant vers le haut
                if (MenuInputInterpreter.isMenuUp())
                {
                    do
                    {
                        selection_y--;
                        if (selection_y < 0)
                            selection_y = items.GetLength(1) - 1;
                    }
                    while (items[selection_x, selection_y] == null);
                }

                // Selection de l'element suivant dans le menu en allant vers le bas
                if (MenuInputInterpreter.isMenuDown())
                {
                    do
                    {
                        selection_y++;
                        if (selection_y >= items.GetLength(1))
                            selection_y = 0;
                    }
                    while (items[selection_x, selection_y] == null);
                }

                // Selection de l'element suivant dans le menu en allant vers la gauche
                if (MenuInputInterpreter.isMenuLeft())
                {
                    do
                    {
                        selection_x--;
                        if (selection_x < 0)
                            selection_x = items.GetLength(0) - 1;
                    }
                    while (items[selection_x, selection_y] == null);
                }

                // Selection de l'element suivant dans le menu en allant vers la droite
                if (MenuInputInterpreter.isMenuRight())
                {
                    do
                    {
                        selection_x++;
                        if (selection_x >= items.GetLength(0))
                            selection_x = 0;
                    }
                    while (items[selection_x, selection_y] == null);

                }
            }

            // Validation d'un element de menu
            if (MenuInputInterpreter.isMenuItemSelected())
                OnItemSelected(selection_x, selection_y);

            // Annulation
            else if (MenuInputInterpreter.isMenuCanceled())
                OnCancelling();
        }
        
        protected void OnItemSelected(int _item_index_x, int _item_index_y)
        {
            if (items != null)
                if (items[_item_index_x, _item_index_y] != null)
                    items[_item_index_x, _item_index_y].OnItemSelected();
        }
        #endregion
    }
}
